package org.lex.dbq;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.ExecutionException;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.lex.dbq.model.bo.JdbcUrl;
import org.lex.dbq.view.session.ConnectSessionDialog;
import org.lex.dbq.view.sql.SqlPane;
import org.lex.dbq.view.template.ConnectTemplateDialog;
import org.lex.jdbc.driver.MyDriverManager;
import org.lex.lang.AggregateException;
import org.lex.swt.SWTWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DBQOperator {
	private Logger log = LoggerFactory.getLogger(DBQOperator.class);

	public DBQApplication getApplication() {
		return DBQApplication.getApplication();
	}

	public Connection getCurrentConnection() {
		Connection con = null;

		TabFolder folder = getApplication().sqlTabFolder;
		TabItem[] items = folder.getSelection();
		if (0 != items.length) {
			SqlPane sql = (SqlPane) items[0].getControl();
			con = sql.getConnection();
		}
		return con;
	}

	public void showConnectSessionDialog(String sessionName) {
		JdbcUrl url = ConnectSessionDialog.showConnect(getShell(), sessionName);
		log.info("session url=" + url);
		if (null != url) {
			doConnect(url);
		}
	}

	public void showConnectTemplateDialog(String templateName) {
		JdbcUrl url = ConnectTemplateDialog.showConnect(getShell(), templateName);
		log.debug("template url=" + url);
		if (null != url) {
			doConnect(url);
		}
	}

	public void doConnect(final JdbcUrl url) {
		new SWTWorker<Connection>() {

			@Override
			protected Connection doInBackground() {
				Connection conn = null;
				try {
					url.getDriver().load();
					conn = MyDriverManager.getConnection(url.getUrl(), url.getProperties());
				} catch (RuntimeException e) {
					e.printStackTrace();
				} catch (AggregateException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				return conn;
			}

			@Override
			protected void done() {
				Connection conn;
				try {
					conn = this.get();
					getApplication().addConnection(url.getDriver().getName(), conn);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}.execute();
	}

	public void doConnect2(final JdbcUrl url) {
		getApplication().getShell().getDisplay().syncExec(new Runnable() {
			public void run() {
				log.info("open url: " + url);
				try {
					url.getDriver().load();
					Connection conn = MyDriverManager.getConnection(url.getUrl(), url.getProperties());
					getApplication().addConnection(url.getDriver().getName(), conn);
				} catch (AggregateException e) {
					log.error("", e);
				} catch (SQLException e) {
					log.error("", e);
				}
			}
		});
	}

	private Shell getShell() {
		return getApplication().getShell();
	}
}
